// @flow
import React, { Component } from 'react'
import { Menu } from 'antd'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import styles from './GlobalMenus.css'
const SubMenu = Menu.SubMenu
type Props = {
  saveGroupId: Function,
  saveDeviceData: Function,
  customerData: Array<Object>,
  history: Object,
  close: Function
}
type State = {
  itemData: Array<Object>
}
export class GlobalMenus extends Component {
  state: State
  toggleSubMenu: () => void
  linkToDeviceCard: Function
  linkToOverView: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      itemData: []
    }
    this.toggleSubMenu = this.toggleSubMenu.bind(this)
    this.linkToDeviceCard = this.linkToDeviceCard.bind(this)
    this.linkToOverView = this.linkToOverView.bind(this)
  }
  toggleSubMenu (e: Object) {
    if (e[0]) {
      const len = e.length - 1
      const item = e[len]
      // $FlowFixMe
      universalFetch(`${__SERVICE_API__}device/customers/${item}/devices`)
        .then((res) => res.json())
        .then((json) => {
          if (json.code !== 200000) {
            throw new Error(JSON.stringify({ code: json.code, message: json.message }))
          }
          this.setState({
            itemData: json.result
          })
        })
        .catch((error) => {
          handleFetchError(error)
          console.error(error)
        })
    }
  }
  linkToDeviceCard (data: Object) {
    const { history } = this.props
    const location = {
      pathname: '/device-card',
      state: {
        data: data,
        groupId: data.id,
        groupName: data.name,
        longitude: data.region.longitude,
        latitude: data.region.latitude,
        count: data.count
      }
    }
    history.push(location)
    this.props.close()
  }
  linkToOverView (data: Object) {
    const { saveDeviceData, history } = this.props
    // saveGroupId(data.id)
    saveDeviceData(data)
    history.push(`/device-file/${data.id}/overview`)
    this.props.close()
  }
  render () {
    const { customerData } = this.props
    const { itemData } = this.state
    const component = this
    return (
      <div className={styles['menuCon']}>
        <Menu style={{ width: 240 }}
          onOpenChange={this.toggleSubMenu}
          mode='vertical'
          theme='dark'
        >
          {
            customerData.map((d, i) => {
              if (d.count === 0) {
                return <Menu.Item key={d.id}>
                  <span onClick={function () { component.linkToDeviceCard(d) }} >
                    {d.name}
                  </span>
                </Menu.Item>
              } else {
                return <SubMenu
                  key={d.id}
                  title={<span onClick={function () { component.linkToDeviceCard(d) }} >
                    {d.name}
                  </span>}>
                  {
                    itemData.map((dd, ii) => {
                      return <Menu.Item key={dd.id}>
                        <span onClick={function () { component.linkToOverView(dd) }} >
                          {dd.name}
                        </span>
                      </Menu.Item>
                    })
                  }
                </SubMenu>
              }
            })
          }
        </Menu>
      </div>
    )
  }
}
export default GlobalMenus
